/**
 * @DESC 该文件用于编辑组装请求地址和请求数据(强智教务系统接口)
 */
const cloudsets = require('application.js').CloudSetting;
const usecloud = cloudsets.UseCloud;
const useesc = cloudsets.UseEsc;
const app = getApp();
function NetError(){
  wx.showToast({
    title: '网络错误',
    icon:'none'
  })
  wx.hideLoading({
    complete: (res) => {},
  })
}
//f封装的云函数请求
function CallCloudFunction(funcname,bodydata){
  return new Promise(function(resolve,reject){
    wx.cloud.callFunction({
      name:funcname,
      data:bodydata,
      success:res=>{resolve(res)},
      fail:res=>{reject('error')
    NetError();}
    })
  })
}
//f封装的后台接口请求
function CallEscApi(apiurl,bodydata,method){
  return new Promise(function(resolve,reject){
    var _method = 'GET';
    if(typeof(method)!='undefined'){
      _method = method;
    }
    wx.request({
      url: apiurl,
      data:bodydata,
      method:_method, //请求方式为可传参数，默认为GET
      success:res=>{resolve(res)},
      fail:res=>{reject('error') 
      NetError();}
    })
  })
}


// 用户身份校验接口组装函数和请求模拟
function AuthUser(xh,pwd,sel_item){
  //用户身份校验，账号密码核验通过后，可以获取token凭证 ,确认密码正确后setStorge设置缓存
  var url = sel_item.api + 'method=authUser&xh=' + xh + '&pwd=' + pwd;
  console.log(url)
  var _ = this;
  return new Promise(function(resolve,reject){
    if(usecloud){
      //使用云开发Environment
      CallCloudFunction('sync',{URL:url}).then(res=>{
        console.log(res)
        if(res.result.flag == 1 || res.result.success){ //强智的后台版本不同，对多版本后台进行适配 两种success 状态的判定
          //账号或密码正确 ， 设置缓存
          wx.setStorageSync('educookie', {xh:xh,pwd:pwd,sel_item:sel_item})
          //设置全局通用变量
          app.token = res.result.token;
          app.sel_item = sel_item;
          app.xh = xh;
          //减少延时
          resolve(res.result)
        }
        
        resolve(res.result)
      }).catch(err=>{
        reject('error') //返回失败
        NetError();
      })
    }else{
      /**
       * @返回参数形式 flag msg
       * @flag 0 / 1
       * @msg 登录成功/账号或密码错误
       */

    }
  })
  
}
// 获取当前学年学期和周次的请求封装
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
};
function getformatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-');
};
function getCurrentTime(sel_item){
  var url = sel_item.api+'method=getCurrentTime&currDate='+getformatTime(new Date());
  return new Promise(function(resolve,reject){
    CallCloudFunction('sync',{URL:url,token:app.token}).then(res=>{
      console.log(res)
      if(res.result.token == -1){
        wx.reLaunch({
          url: '/pages/Door/Login/Login',
        })
      }else{
        resolve(res)
      }
    }).catch(err=>{
      console.log(err)
      reject(err) //返回失败
      NetError();
    })
  })
}
// 获取当前周次的课程
function getKbcxAzc(sel_item,xqzc,xh){
  var url = sel_item.api + 'method=getKbcxAzc&xh='+xh +'&xnxqid='+xqzc.xnxqh +'&zc=' + xqzc.zc; 
  return new Promise(function(resolve,reject){
    CallCloudFunction('sync',{URL:url,token:app.token}).then(res=>{
      console.log(res)
      if(res.result.token == -1){
        wx.reLaunch({
          url: '/pages/Door/Login/Login',
        })
      }else{
        resolve(res)
      }
    }).catch(err=>{
      reject(err) //返回失败
    })
  })
};
//获取该教学楼 空闲教室
function getKxJscx(sel_item,timepart,jxlid){
  var url = sel_item.api + 'method=getKxJscx&time=' +getformatTime(new Date())+'&idleTime='+timepart+'&jxlid='+jxlid;
  
  return new Promise(function(resolve,reject){
    CallCloudFunction('sync',{URL:url,token:app.token}).then(res=>{
     // console.log(res)
     if(res.result.token == -1){
      wx.redirectTo({
        url: '/pages/Door/Login/Login',
      })
    }else{
      resolve(res)
    }
    }).catch(err=>{
      reject(err) //返回失败
    })
  })
}
//获取学生成绩
function getCjcx(sel_item){
  var url = sel_item.api + 'method=getCjcx&xh='+app.xh;
  return new Promise(function(resolve,reject){
    CallCloudFunction('sync',{URL:url,token:app.token}).then(res=>{
      //结果过滤器
      console.log(res)
     if(res.result.token == -1){
       wx.redirectTo({
         url: '/pages/Door/Login/Login',
       })
     }
      resolve(res)
    }).catch(err=>{
      reject(err) //返回失败
    })
  })
}
module.exports={
  AuthUser:AuthUser,
  getCurrentTime:getCurrentTime,
  getKbcxAzc:getKbcxAzc,
  getKxJscx:getKxJscx,
  getCjcx:getCjcx
}